package com.starbaba.credit.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.starbaba.base.dao.LoanManagerBaseDao;
import com.starbaba.base.service.CommProductPrdidService;
import com.starbaba.base.service.impl.CommProductPrdidServiceImpl;
import com.starbaba.credit.service.CreditOrderService;
import com.tuanzi.server.loan_base.model.db.credit.PayOrder;
import com.tuanzi.server.loan_base.util.DateUtils;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;

/**
 * 征信订单Service
 * @author chenweijun
 *
 */
public class CreditOrderServiceImpl extends LoanManagerBaseDao implements CreditOrderService{
	
	private static final String ALL_CHANNEL = "9999";
	private CommProductPrdidService commProductPrdidService = new CommProductPrdidServiceImpl();
		
	@Override
	public List<PayOrder> getCreditOrderList(Map<String, Object> params,
			int pageNo, int pageSize) {
		
		StringBuilder sql = new StringBuilder("select a.*, b.name AS prd_name from `pay_order` AS a LEFT JOIN comm_product_prdid AS b ON a.prd_id = b.prdid where 1=1 ");
		String whereSql = generateWhereSql(params);
		sql.append(whereSql)
		   .append(" order by create_time desc ")
		   .append(" limit " + pageNo + "," + pageSize);
		List<PayOrder> list = new ArrayList<PayOrder>();
		try {
			 list =  getDao().list(sql.toString(), PayOrder.class);
			 if(list != null && list.size() != 0) {
				 for(PayOrder payOrder : list) {
					payOrder.setPhone_num(PhoneNumNideUtil.hideNum(payOrder.getPhone_num()));
					payOrder.setUser_name(PhoneNumNideUtil.hideName(payOrder.getUser_name()));
					payOrder.setId_card(PhoneNumNideUtil.hideIdCard(payOrder.getId_card()));
				 }
			 }
		} catch (AppException e) {
			LogService.error(e);
		}
		return list;
	}
	

	@Override
	public int getCreditOrderCount(Map<String, Object> params) {
		
		StringBuilder sql = new StringBuilder("select count(1) from `pay_order` where 1=1 ");
		String whereSql = generateWhereSql(params);
		sql.append(whereSql);
		int result = 0;
		try {
			result =  getDao().getInt(sql.toString());
		} catch (AppException e) {
			LogService.error(e);
		}
		return result;
	}

	@Override
	public int updateCreditOrderStatus(Map<String, Object> paramsMap) {
		
		String sql = "UPDATE `pay_order` SET order_status = ? , operator = ? , operate_time = ? WHERE id = ?";
		int result = 0;
		try {
			result = getDao().update(sql, paramsMap.get("order_status"), paramsMap.get("operator"), new Date(), paramsMap.get("id"));
		} catch (AppException e) {
			LogService.error(e);
			return result;
		}
		return result;
	}
	
	@Override
	public String exportCreditOrderList(Map<String, Object> params) {
		
		StringBuilder sql = new StringBuilder("select a.*, b.name AS prd_name from `pay_order` AS a LEFT JOIN comm_product_prdid AS b ON a.prd_id = b.prdid where 1=1 ");
		String whereSql = generateWhereSql(params);
		sql.append(whereSql);
		List<PayOrder> list = new ArrayList<PayOrder>();
		try {
			list = getDao().list(sql.toString(), PayOrder.class);
		} catch (AppException e) {
			LogService.error(e);
		}
		
		StringBuilder sb = new StringBuilder("创建订单时间,订单号,手机号,平台,激活产品,激活渠道,征信入口,套餐版本,付费金额,支付渠道,状态,操作人\r\n");
		if(list == null) {
			return sb.toString();
		}
		
		for(PayOrder order: list){
			String creditTypeName = "";
			if(order.getCredit_type() != null){
				creditTypeName = getCreditTypeName(order.getCredit_type());
			}
			
			String payName = "";
			if(order.getPay_type() != null){
				payName = getPayName(order.getPay_type());
			}
			String orderStatusName = getOrderStatusName(order.getOrder_status());
			
			sb.append(order.getCreate_time() + ",")
			  .append(order.getOrder_id() + ",")
			  .append(PhoneNumNideUtil.hideNum(order.getPhone_num()) + ",")
			  .append(order.getPlatform() + ",")
			  .append(order.getPrd_name() + ",")
			  .append(order.getChannel_name() + ",")
			  .append(order.getEntrance() + ",")
			  .append(creditTypeName + ",")
			  .append(order.getOrder_pay_amount() + ",")
			  .append(payName + ",")
			  .append(orderStatusName + ",")
			  .append(order.getOperator() + "\r\n");
		}
		
		return sb.toString();
		
	}
	
	/**
	 * 拼接查询语句where部分
	 * @param paramsMap
	 * @return
	 */
	private String generateWhereSql(Map<String, Object> paramsMap){
		
		String platform = (String)paramsMap.get("platform");
		String prd_id = (String) paramsMap.get("prdid");
		String channel = (String) paramsMap.get("channel");
		String pay_type = (String) paramsMap.get("pay_type");
		String user_id = (String) paramsMap.get("user_id");
		String phone_num = (String) paramsMap.get("phone_num");
		String user_name = (String) paramsMap.get("user_name");
		String id_card = (String)  paramsMap.get("id_card");
		String credit_type = (String) paramsMap.get("credit_type");
		String order_status = (String) paramsMap.get("order_status");
		String startDate = (String) paramsMap.get("startDate");
		String endDate = (String) paramsMap.get("endDate");
		String entrance = (String) paramsMap.get("entrance");
		
		StringBuilder sql = new StringBuilder();
		
		if(StringUtils.isNotEmpty(platform)){
			sql.append(" and platform = '" + platform + "'");
		}
		
		if(StringUtils.isNotEmpty(prd_id)){
			sql.append(" and prd_id = " + prd_id );
		}
		
		if(StringUtils.isNotEmpty(channel) && !channel.equals(ALL_CHANNEL)){
			sql.append(" and channel = '" + channel + "'");
		}
		
		if(StringUtils.isNotEmpty(pay_type)){
			sql.append(" and pay_type = " + pay_type );
		}
		
		if(StringUtils.isNotEmpty(user_id)){
			sql.append(" and user_id = '" + user_id + "'");
		}
		
		if(StringUtils.isNotEmpty(phone_num)){
			sql.append(" and phone_num = '" + phone_num + "'");
		}
		
		if(StringUtils.isNotEmpty(user_name)) {
			sql.append(" and user_name = '" + user_name + "'");
		}
		
		if(StringUtils.isNotEmpty(id_card)) {
			sql.append(" and id_card = '" + id_card + "'");
		}
		
		if(StringUtils.isNotEmpty(credit_type)){
			sql.append(" and credit_type = " + credit_type );
		}
		
		if(StringUtils.isNotEmpty(order_status)){
			sql.append(" and order_status = " + order_status );
		}
		
		if(StringUtils.isNotEmpty(startDate)){
			sql.append(" and create_time >= '" + DateUtils.getDateStartTime(startDate) + "'");
		}
		
		if(StringUtils.isNotEmpty(endDate)){
			sql.append(" and create_time <= '" + DateUtils.getDateEndTime(endDate) + "'");
		}
		
		if(StringUtils.isNotEmpty(entrance)){
			sql.append(" and entrance = " + entrance );
		}
		
		return sql.toString();
	}
	
	
	/**
	 * 返回支付方式id对应的名称
	 * @param pay_type
	 * @return
	 */
	private String getPayName(int pay_type){
		String payName = "";
		switch (pay_type) {
			case 1:
				payName = "支付宝APP支付";
				break;
				
			case 2:
				payName = "微信公众号支付";
				break;
				
			case 3:
				payName = "微信H5支付";
				break;
			
			case 6:
				payName = "支付宝电脑网站支付";
				break;
				
			case 7:
				payName = "支付宝H5支付";
				break;
				
			case 8:
				payName = "连连微信公众号支付";
				break;
				
			default:
				payName = "未定义";
				break;
		}
		return payName;
	}
	
	/**
	 * 返回征信套餐类型名称
	 * @param creditType
	 * @return
	 */
	private String getCreditTypeName(int creditType){
		String creditTypeName = "";
		switch (creditType) {
		case 1:
			creditTypeName = "基础版";
			break;
			
		case 2:
			creditTypeName = "高级版";
			break;

		case 3:
			creditTypeName = "升级版";
			break;
			
		case 5:
			creditTypeName = "通讯行为分析版本";
			break;
			
		case 21:
			creditTypeName = "精准通讯版";
			break;
			
		case 22:
			creditTypeName = "完整版";
			break;
			
		default:
			break;
		}
		
		return creditTypeName;
		
	}
	
	/**
	 * 返回订单状态对应的名称
	 * @param order_status
	 * @return
	 */
	private String getOrderStatusName(int order_status){
		
		String orderStatusName = "";
		switch (order_status) {
			case -1:
				orderStatusName = "未支付";
				break;
				
			case 0:
				orderStatusName = "待支付";
				break;
				
			case 1:
				orderStatusName = "已支付";
				break;
				
			case 2:
				orderStatusName = "已完成";
				break;
				
			case 3:
				orderStatusName = "退款中";
				break;
				
			case 4:
				orderStatusName = "已退款";
				break;

			default:
				orderStatusName = "未定义";
				break;
		}
		return orderStatusName;
		
	}


}
